
package com.john.blog.controller.back;

import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.john.blog.common.utils.DetectUtils;
import com.john.blog.common.vo.CommonResult;
import com.john.blog.controller.back.support.AbstractBackModelControllerSupport;
import com.john.blog.model.po.Blog;
import com.john.blog.model.po.Category;
import com.john.blog.service.BlogService;
import com.john.blog.service.CategoryService;

import tk.mybatis.mapper.entity.Example;

/**
 * 文章分类controller
 * 
 * @author JiangGengchao
 * @date 2018年2月25日
 */
@RestController
@RequestMapping("/blog/back/category")
public class BackCategoryController extends AbstractBackModelControllerSupport<Category> {

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private BlogService blogService;

    @PostConstruct
    public void init() {
        this.service = categoryService;
    }

    @RequestMapping("/add")
    @Override
    public CommonResult add(Category model) {
        model.setBlogNum(0);
        return super.add(model);
    }

    @RequestMapping("/del")
    @Override
    public CommonResult del(String id) {
        Example example = new Example(Blog.class);
        example.createCriteria().andEqualTo("categoryID", id);
        List<Blog> blogList = blogService.selectByExample(example);
        if (DetectUtils.notEmpty(blogList)) {
            return CommonResult.error("存在使用该分类的的博文，禁止删除该分类");
        } else {
            return super.del(id);
        }
    }

}
